package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Notice;
import com.example.entity.Params;
import com.example.mapper.NoticeMapper;
import com.example.service.NoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void add(Notice notice) {
        notice.setTime(DateUtil.now());
        noticeMapper.insert(notice);
    }

    @Override
    public void updateEntity(Notice notice) {
        notice.setTime(DateUtil.now());
        noticeMapper.updateById(notice);
    }

    @Override
    public PageInfo<Notice> search(Params params) {
        //1.开启分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //2.查询
        List<Notice> list = noticeMapper.findBySearch(params);
        //3.封装
        PageInfo<Notice> pageInfo = PageInfo.of(list);
        return pageInfo;
    }

    @Override
    public List<Notice> findNotice() {
        return noticeMapper.findNotice();
    }
}
